import axios from 'axios';
// import Qs from 'qs'
import router from '@/router/index';
import store from '@/store/index';
import { Message } from 'element-ui';

const service = axios.create({
    baseURL: GlobalConfig.apiBaseurl + 'OverrunService/',
    timeout: 30000,
    responseType: 'json'
    // withCredentials: true
});

service.interceptors.request.use(
    request => {
        // request.headers['Content-Type'] = 'text/plain';
        request.headers['Content-Type'] = 'application/json';
        request.headers['Authorization'] = store.state.user.token;
        request.headers['token'] = store.state.user.token;
        if (request.method == 'post') {
            if (request.data instanceof FormData) {
                if (store.getters['user/isLogin']) {
                    // 如果是 FormData 类型（上传图片）
                    // request.data.append('AutoLogin', store.state.user.token);
                    request.data.append('LoginName', store.state.user.account);
                }
            } else {
                // 带上 token
                if (request.data == undefined) {
                    request.data = {};
                }
                if (store.getters['user/isLogin']) {
                    request.data.AutoLogin = store.state.user.token;
                    request.data.LoginName = store.state.user.account;
                }
                // request.data = Qs.stringify(request.data)
            }
        } else {
            // 带上 token
            if (request.params == undefined) {
                request.params = {};
            }
            if (store.getters['user/isLogin']) {
                request.params.AutoLogin = store.state.user.token;
                request.params.LoginName = store.state.user.account;
            }
        }
        return request;
    }
);

service.interceptors.response.use(
    response => {
        const res = response.data;
        if (res.status !== '0' && res.status != '1001' && res.res.msg != '服务器错误：token验证失败，请重新登录') {
            let errmsg = res.errmsg;
            if (errmsg) {
                Message({
                    message: errmsg,
                    type: 'error',
                    duration: 3 * 1000
                });
            }
            return Promise.reject(response.data);
        } else if (res.res && res.res.LoginStatus == 'F'|| res.res.msg=='服务器错误：token验证失败，请重新登录') {

            if (store.getters['user/isLogin']) {
                store.dispatch('user/logout').then(() => {
                    Message({
                        message: '登录信息失效，请重新登录',
                        type: 'error',
                        duration: 3 * 1000
                    });
                    router.push({
                        path: '/login',
                        query: {
                            redirect: router.currentRoute.fullPath
                        }
                    });
                });
            }

            return Promise.reject('登录信息失效，请重新登录');
        } else {
            return router.t(response);
        }
    },
    error => {
        return Promise.reject(error);
    }
);

export default service;
